<?php

class GoodsGeometry extends MrMongoDocument {
	public $geometry;
	public $gTypeId;
	public $goodsName;
	public $description;
	public $thumbPath;
	public $dependId;
	/**
	 * This method have to be defined in every Model
	 * @return string MongoDB collection name, witch will be used to store documents of this model
	 */
	public function getCollectionName() {
		return 'goodsGeometry';
	}
	
	public function rules() {
		// return array (array ('gTypeId', 'EMongoUniqueValidator', 'on' => array ('insert' ) ),array ('goodsName', 'EMongoUniqueValidator', 'on' => array ('insert' ) )  );
		return array();
	}
	
	public static function model($className = __CLASS__) {
		return parent::model ( $className );
	}

	public function getFindBygTypeId($gTypeId) {
		return self::model()->findAllByAttributes(array('gTypeId'=>$gTypeId));
	}
	
	public function getAllFetchPage($currentPageNo,$recordsPerPage){
		
		$offset=($currentPageNo - 1) * $recordsPerPage;
		$criteria = new EMongoCriteria;
		$criteria->limit($recordsPerPage)->offset($offset);
		// $criteria->limit(1);
	
		$record= self::model()->findAll($criteria);
		return $record;
	}
	
	public function getFindBygTypeIdOnPage($gTypeId,$currentPageNo,$recordsPerPage){
		// SetSkip((currentPage - 1) * recordsPerPage).SetLimit(recordsPerPage);
		// $record= self::model()->findAllByAttributes(array('gTypeId'=>$gTypeId),array("$limit"))->limit(10)->skip(1);
		$offset=($currentPageNo - 1) * $recordsPerPage;
		$criteria = new EMongoCriteria;
		$criteria->gTypeId('==',$gTypeId)->limit($recordsPerPage)->offset($offset);
		// $criteria->limit(1);
	
		$record= self::model()->findAll($criteria);
		return $record;
		// return self::model()->findAllByAttributes(array('gTypeId'=>$gTypeId));
	}
	
 public function getFindBygTypeIdAndDependIdOnPage($gTypeId,$dependId,$currentPageNo,$recordsPerPage){
    // SetSkip((currentPage - 1) * recordsPerPage).SetLimit(recordsPerPage);
    // $record= self::model()->findAllByAttributes(array('gTypeId'=>$gTypeId),array("$limit"))->limit(10)->skip(1);
    $offset=($currentPageNo - 1) * $recordsPerPage;
    $criteria = new EMongoCriteria;
    $criteria->gTypeId('==',$gTypeId)->dependId('==',$dependId)->limit($recordsPerPage)->offset($offset);
    // $criteria->limit(1);
  
    $record= self::model()->findAll($criteria);
    return $record;
    // return self::model()->findAllByAttributes(array('gTypeId'=>$gTypeId));
  }
    
	public function findById($id) {
		$id = new MongoId ( $id );
		return self::model()->findAllByAttributes(array('_id'=>$id));
	}

	public function findByGoods($goods) {
		
		$ids=array();
		
		foreach($goods as $hg){
			$id = new MongoId ($hg['gid']['$id']);
			// $id = new MongoId ( $id );
			array_push($ids,$id);
		}
		echo json_encode($ids);
		
		// { field : { $in : array } }
		// $db->users->find(array('$or' => array(array("a" => 1), array("b" => 2))));
		// $db->users->find(array("age" => array('$gt' => 33, '$lte' => 40)));
		// return self::model()->findAllByAttributes(array('_id'=>$ids));
		return self::model()->findAllByAttributes(array('_id'=>array('$in' => $ids)));
	}
	
}